package com.huluobo.a2206douyindemo.mine;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.base.bean.UserBeanResult;
import com.huluobo.base.bean.VideoBeanResult;
import com.huluobo.base.mvvm.BaseRepo;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by LC on 2024/8/21.
 */
public class MineRepo extends BaseRepo {
    public void queryCollectVideos(MutableLiveData<List<VideoBeanResult.VideoBean>> success,
                                   MutableLiveData<String> failed) {
        db.getVideoDao().queryVideos()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<VideoBeanResult.VideoBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<VideoBeanResult.VideoBean> videoBeans) {
                        success.setValue(videoBeans);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getUserInfo(String username, MutableLiveData<UserBeanResult.UserBean> success,
                            MutableLiveData<String> failed) {
        UserBeanResult.UserBean userBean = db.getUserDao().queryUserInfoByName(username);
        if (userBean != null) {
            success.setValue(userBean);
        } else {
            failed.setValue("查询用户失败");
        }
    }
}
